package com.wzx.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzx.basics.baseVo.PageVo;
import com.wzx.basics.baseVo.Result;
import com.wzx.basics.utils.PageUtil;
import com.wzx.basics.utils.ResultUtil;
import com.wzx.data.utils.NullUtils;
import com.wzx.data.entity.Folder;
import com.wzx.data.service.IFolderService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 文件夹 前端控制器
 * @author wzx
 * @since 2023/03/20
 */
@Slf4j
@RestController
@Api(tags = "文件夹管理接口")
@RequestMapping("/wzx/folder")
@Transactional
public class FolderController {

    @Autowired
    private IFolderService iFolderService;

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条文件夹")
    public Result<Folder> get(@RequestParam String id){
        return new ResultUtil<Folder>().setData(iFolderService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部文件夹个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iFolderService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部文件夹")
    public Result<List<Folder>> getAll(){
        return new ResultUtil<List<Folder>>().setData(iFolderService.list());
    }


    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改文件夹")
    public Result<Folder> saveOrUpdate(Folder folder){
        LambdaQueryWrapper<Folder> lambda = new LambdaQueryWrapper<Folder>();
        lambda.eq(Folder::getName,folder.getName());
        Folder one = iFolderService.getOne(lambda);
        if(one!=null){
            return ResultUtil.error("该文件夹已存在");
        }
        iFolderService.saveOrUpdate(folder);
        return  ResultUtil.success();
    }


    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除文件夹")
    public Result<Object> delByIds(@RequestParam String id){
        iFolderService.removeById(id);
        return ResultUtil.success();
    }
}
